package com.ggh.jinjilive.dialog;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.cy.dialog.BaseDialog;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.common.MerchantSession;
import com.ggh.jinjilive.dialog.adapter.GiftListAdapter;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.listener.AddOnClickListener1;
import com.ggh.jinjilive.mobile.live.GiftEntityMobile;
import com.ggh.jinjilive.mobile.live.GiftMobile;
import com.ggh.jinjilive.mobile.live.GiftSouvenirMobile;
import com.ggh.jinjilive.mobile.live.SendGiftMobole;
import com.ggh.jinjilive.utils.GeneralUtils;
import com.ggh.jinjilive.utils.loginIMUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 礼物列表
 */
public class GiftListDialog extends BaseDialog {
    private GiftEntityMobile giftEntityMobile;
    //钻石
    private TextView myDiamondNumText;
    //礼物列表
    private RecyclerView giftListView;
    //数量
    private EditText sendMultipleGiftText;

    private GiftListAdapter giftListAdapter;
    /**
     * 送礼物的弹窗
     */
    private String myDiamondNum = "0";
    //选中的数据
    private GiftSouvenirMobile pitchon;
    private int giftNum;

    public GiftListDialog(Context context) {
        super(context);

    }

    public GiftListDialog(Context context, int themeResId) {
        super(context, themeResId);

    }
    private int be_user_id;
    private int live_id;
    public void roomData(int be_user_id,int live_id){
        this.be_user_id = be_user_id;
        this.live_id = live_id;
    }

    private String rivalId ="0";
    private String pkStartTime;
    public void pKRoomData(String rivalId,String pkStartTime){
        this.rivalId = rivalId;
        this.pkStartTime = pkStartTime;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        contentView(R.layout.dialog_gift_list);
        canceledOnTouchOutside(true);
        gravity(Gravity.BOTTOM);

        //钻石
        myDiamondNumText = findViewById(R.id.myDiamondNumText);
        //数量
        sendMultipleGiftText = findViewById(R.id.sendMultipleGiftText);
        //礼物列表
        giftListView = findViewById(R.id.giftListView);
        giftListView.setLayoutManager(new LinearLayoutManager(getContext()));
        giftListAdapter = new GiftListAdapter(getContext());
        giftListAdapter.setAddItemOnClickListener(new AddItemOnClickListener() {
            @Override
            public void onSkipClicked(int position, Object mObject) {
                GiftSouvenirMobile mGiftSouvenirMobile = (GiftSouvenirMobile) mObject;
                if(null != mGiftSouvenirMobile){
                    for (int i = 0;i < giftEntityMobile.getList().size(); i++){
                        GiftMobile giftMobile = giftEntityMobile.getList().get(i);
                        for (int a = 0;a < giftMobile.getList().size(); a++){
                            GiftSouvenirMobile giftSouvenirMobile = giftMobile.getList().get(a);
                            if(giftSouvenirMobile.getGift_id() == mGiftSouvenirMobile.getGift_id()){
                                pitchon = giftSouvenirMobile;
                                giftEntityMobile.getList().get(i).getList().get(a).setSelected(true);
                            }else {
                                giftEntityMobile.getList().get(i).getList().get(a).setSelected(false);
                            }
                        }
                    }
                }
                giftListAdapter.notifyDataSetChanged();
            }
        });
        giftListView.setAdapter(giftListAdapter);
        //获取礼物列表
        GeneralUtils.giftlist(new AddOnClickListener1() {
            @Override
            public void onSkipClicked(Object mObject) {
                giftEntityMobile = (GiftEntityMobile) mObject;
                if(null != giftEntityMobile){
                    //钻石
                    myDiamondNumText.setText(giftEntityMobile.getMy_diamond_wallet());
                    giftListAdapter.GiftData(giftEntityMobile.getList());
                }

            }
        });
        //充值
        findViewById(R.id.tv_top_up).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtils.make().show("充值");
            }
        });
        //发送
        findViewById(R.id.sendButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                myDiamondNum = giftEntityMobile.getMy_diamond_wallet();
                if(null != pitchon){
                    int price = pitchon.getPrice();
                    if (TextUtils.isEmpty(sendMultipleGiftText.getText().toString())){
                        giftNum = 1;
                    }else{
                        giftNum = Integer.parseInt(sendMultipleGiftText.getText().toString());
                    }
                    BigDecimal bigDecimal = new BigDecimal(price * giftNum);  // A
                    int result = bigDecimal.compareTo(new BigDecimal(myDiamondNum)); // B
                    if (result == 1) {//A>B
                        ToastUtils.make().show("余额不足");
                        return;
                    }
                    Map<String, Object> map = new HashMap<>();
                    map.put("user_id", MerchantSession.getInstance().getUser().getUser_id());
                    map.put("live_id",live_id);
                    map.put("number",giftNum);
                    map.put("be_user_id",be_user_id +"");
                    map.put("gift_id",pitchon.getGift_id());
                    map.put("pk_user_id",rivalId);
                    map.put("pk_time",pkStartTime);
                    GeneralUtils.givingGift(map, new AddOnClickListener1() {
                        @Override
                        public void onSkipClicked(Object mObject) {
                            if(null != mObject){
                                //获取礼物列表
                                GeneralUtils.giftlist(new AddOnClickListener1() {
                                    @Override
                                    public void onSkipClicked(Object mObject) {
                                        GiftEntityMobile giftEntityMobile = (GiftEntityMobile) mObject;
                                        if(null != giftEntityMobile){
                                            //钻石
                                            myDiamondNumText.setText(giftEntityMobile.getMy_diamond_wallet());
                                        }
                                    }
                                });
                                if(null != addItemOnClickListener){
                                    addItemOnClickListener.onSkipClicked(giftNum,mObject);
                                }
                            }
                        }
                    });
                }else {
                    ToastUtils.make().show("请选择礼物");
                }
            }
        });
    }


    @Override
    public void show() {
        super.show();
        Window window = getWindow();
        //设置弹出位置
        window.setGravity(Gravity.BOTTOM);
        //设置对话框大小
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    private AddItemOnClickListener addItemOnClickListener;
    public void setAddItemOnClickListener(AddItemOnClickListener addItemOnClickListener){
        this.addItemOnClickListener = addItemOnClickListener;
    }
}
